package com.zhangheng.arktspage.dialog

import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.ui.components.JBTextField
import com.intellij.uiDesigner.core.GridConstraints
import com.intellij.uiDesigner.core.GridLayoutManager
import com.intellij.util.ui.JBUI
import java.awt.Dimension
import javax.swing.*

class CreateArkTSDialog(
        project: Project,
        private val path: String
) : DialogWrapper(project) {

    // 页面名输入框
    private val pageNameTextField = JBTextField()
    // 开发者名输入框
    private val pageDeveloperTextField = JBTextField()
    // 页面描述输入框
    private val pageDescTextField = JBTextField()

    private val preferencesUtil: PreferencesUtil = PreferencesUtil()

    init {
        title = "Create ArkTS Page"
        isResizable = false
        init()
    }

    override fun createCenterPanel(): JComponent {
        // 创建内容布局
        val panel = JPanel()
        val gridLayoutManager = GridLayoutManager(10, 13, JBUI.insets(10), -1, -1)
        gridLayoutManager.hGap = 10
        panel.layout = gridLayoutManager

        panel.add(JLabel("Page Name"), GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false))
        panel.add(pageNameTextField, GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, Dimension(200, -1), null, 0, false))

        panel.add(JLabel("Page Developer"), GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false))
        panel.add(pageDeveloperTextField, GridConstraints(4, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, Dimension(200, -1), null, 0, false))

        panel.add(JLabel("Page Description"), GridConstraints(6, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false))
        panel.add(pageDescTextField, GridConstraints(6, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, Dimension(200, -1), null, 0, false))

        panel.maximumSize = Dimension(800, 600)

        pageNameTextField.text = preferencesUtil.getValue(preferencesUtil.pageName)
        pageDeveloperTextField.text = preferencesUtil.getValue(preferencesUtil.developName)
        return panel
    }

    private fun showErrorDialog(message: String) {
        JOptionPane.showMessageDialog(
                null,
                message,
                "Error",
                JOptionPane.ERROR_MESSAGE
        )
    }

    override fun doOKAction() {
        if (pageNameTextField.text.isBlank()) {
            showErrorDialog("Page name cannot be empty")
            return
        }
        if (pageDeveloperTextField.text.isBlank()) {
            showErrorDialog("Page Developer cannot be empty")
            return
        }
        if (pageDescTextField.text.isBlank()) {
            showErrorDialog("Page Description cannot be empty")
            return
        }
        preferencesUtil.setValue(preferencesUtil.pageName, pageNameTextField.text)
        preferencesUtil.setValue(preferencesUtil.developName, pageDeveloperTextField.text)

        super.doOKAction()
    }

    fun getPageName(): String {
        return pageNameTextField.text
    }
    fun getPageDeveloper(): String {
        return pageDeveloperTextField.text
    }
    fun getPageDescription(): String {
        return pageDescTextField.text
    }

}